﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd. 
//--------------------------------------------------------------------

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace DotNet.WinForm.User
{
    using DotNet.Model;
    using DotNet.Utilities;
    using DotNet.Service;
    using DotNet.WinForm.Utilities;

    public partial class FrmUserPermission : BaseForm
    {
        public FrmUserPermission()
        {
            InitializeComponent();
        }

        #region public override void ShowEntity() 显示内容
        /// <summary>
        /// 显示内容
        /// </summary>
        public override void ShowEntity()
        {
            // 绑定操作员信息
            this.txtUserName.Text = UserInfo.UserName;
            this.txtFullName.Text = UserInfo.RealName;
            this.txtRole.Text = UserInfo.RoleName;
            this.txtCompanyCode.Text = UserInfo.CompanyCode;
            this.txtCompanyFullName.Text = UserInfo.CompanyFullName;
            this.txtDepartmentCode.Text = UserInfo.DepartmentCode;
            this.txtDepartmentFullName.Text = UserInfo.DepartmentFullName;
            // 绑定操作员权限
            BaseInterfaceLogic.DataTableAddColumn(ClientCache.Instance.DTPermission, BaseBusinessLogic.SelectedColumn);
            this.grdPermisson.AutoGenerateColumns = false;
            this.grdPermisson.DataSource = ClientCache.Instance.DTPermission.DefaultView;
        }
        #endregion

        #region public override void SetControlState() 设置按钮状态
        /// <summary>
        /// 设置按钮状态
        /// </summary>
        public override void SetControlState()
        {
            if (ClientCache.Instance.DTPermission.DefaultView.Count > 0)
            {
                this.btnSelectAll.Enabled = true;
                this.btnInvertSelect.Enabled = true;
                this.btnExport.Enabled = true;
                this.btnRemove.Enabled = true;
            }
            else
            {
                this.btnSelectAll.Enabled = false;
                this.btnInvertSelect.Enabled = false;
                this.btnExport.Enabled = false;
                this.btnRemove.Enabled = false;
            }
        }
        #endregion   

        private void FrmUserPermission_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 116:
                    ClientCache.Instance.DTPermission = null;
                    // 重新获取权限
                    ClientCache.Instance.GetPermission(UserInfo);
                    // 点击了F5按钮
                    this.FormOnLoad();
                    break;
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataRow dataRow in ClientCache.Instance.DTPermission.Rows)
            {
                dataRow[BaseBusinessLogic.SelectedColumn] = true.ToString();
            }
        }

        private void btnInvertSelect_Click(object sender, EventArgs e)
        {
            foreach (DataRow dataRow in ClientCache.Instance.DTPermission.Rows)
            {
                if (dataRow[BaseBusinessLogic.SelectedColumn].ToString() == true.ToString())
                {
                    dataRow[BaseBusinessLogic.SelectedColumn] = false.ToString();
                }
                else
                {
                    dataRow[BaseBusinessLogic.SelectedColumn] = true.ToString();
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            this.ExportExcel(this.grdPermisson, @"\Modules\Export\", "权限.csv");
        }

        #region private string[] GetSelecteIds() 获得已被选择的权限主键数组
        /// <summary>
        /// 获得已被选择的权限主键数组
        /// </summary>
        /// <returns>主键数组</returns>
        private string[] GetSelecteIds()
        {
            return BaseInterfaceLogic.GetSelecteIds(ClientCache.Instance.DTPermission, BasePermissionItemTable.FieldId, BaseBusinessLogic.SelectedColumn);
        }
        #endregion

        #region public override int BatchDelete() 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        public override int BatchDelete()
        {
            int returnValue = 0;
            if (MessageBox.Show(AppMessage.MSG0075, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                string[] ids = this.GetSelecteIds();
                // 设置表主键
                DataColumn[] keys = new DataColumn[1];
                keys[0] = ClientCache.Instance.DTPermission.Columns[BasePermissionItemTable.FieldId];
                ClientCache.Instance.DTPermission.PrimaryKey = keys;
                for (int i = 0; i < ids.Length; i++)
                {
                    ClientCache.Instance.DTPermission.Rows.Remove(ClientCache.Instance.DTPermission.Rows.Find(ids[i]));
                    returnValue++;
                }
            }
            this.SetControlState();
            return returnValue;
        }
        #endregion

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // 检查批量输入的有效性
            if (BaseInterfaceLogic.CheckInputSelectAnyOne(ClientCache.Instance.DTPermission, BaseBusinessLogic.SelectedColumn))
            {
                this.BatchDelete();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            // 关闭窗体
            this.Close();
        }

        private void grdPermisson_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //定义一个画笔，颜色用行标题的前景色填充
            SolidBrush brush = new SolidBrush(this.grdPermisson.RowHeadersDefaultCellStyle.ForeColor);
            //得到当前行的行号
            int rowIndex = e.RowIndex + 1;
            //DataGridView的RowHeadersWidth 为了算中间位置
            int rowHeadersWidth = grdPermisson.RowHeadersWidth;
            //根据宽度与显示的字符数计算中间位置
            int rowHeadersX = (rowHeadersWidth - rowIndex.ToString().Length * 6) / 2;
            int rowHeadersY = e.RowBounds.Location.Y + 4;
            e.Graphics.DrawString((rowIndex).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.grdPermisson.DefaultCellStyle.Font, brush, rowHeadersX, rowHeadersY);
        }
    }
}